<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\jia;

use app\common\model\jia\Fapiao as FapiaoModel;
use think\facade\Filesystem;
/**
 * 分销商入驻申请模型
 * Class Fapiao
 * @package app\store\model\dealer
 */
class Fapiao extends FapiaoModel
{
    /**
     * 获取分销商申请列表
     * @param string $search
     * @return \think\Paginator
     */
    public function getList(string $search = ''): \think\Paginator
    {
        // 检索查询条件
        $filter = [];
        if (! empty($search)) {
            $filter[] = ['user.nick_name|apply.real_name|apply.mobile', 'like', "%{$search}%"];
        }
        // 查询列表记录
        return $this->alias('apply')
            ->field(['apply.*'])
            ->with(['user.avatar'])
            ->where($filter)
            ->join('user', 'user.user_id = apply.user_id')
            ->order(['apply.status' => 'asc', 'apply.create_time' => 'desc'])
            ->paginate(15);
    }

    /**
     * 分销商入驻审核
     * @param array $data
     * @return bool
     */
    public function submit(array $data): bool
    {
        if ($data['status'] == 2 && empty($data['reject_reason'])) {
            $this->error = '请填写驳回原因';
            return false;
        }
       
        $this->transaction(function () use ($data) {
            if ($data['status'] == 1) {
                $data['path'] = $this->writeFile('pathFile','pdf');
            }
            // 更新申请记录
            $data['audit_time'] = time();
            $this->save($data);
        });
        return true;
    }
 

    /**
     * 写入证书文件
     * @param string $method 支付方式类型
     * @param string $name 证书名称
     * @param string $ext 文件后缀
     * @return string|false
     */
    private function writeFile(string $name, string $ext)
    {
        // 接收上传的文件
        $file = request()->file($name);
        if (empty($file)) {
        	  $this->error = '请上传发票';
          
            return false;
        }
        // 证书文件夹路径
        $dirPath = "fapiao/" . self::$storeId;
        // 证书文件名
        $fileName = $this->buildCertFileName($name, $ext);
        // 写入到本地服务器
        Filesystem::disk('public')->putFileAs($dirPath, $file, $fileName);
        return $fileName;
    }

    /**
     * 生成证书文件名
     * @param string $name
     * @param string $ext
     * @return string
     */
    private function buildCertFileName(string $name, string $ext): string
    {
        $hash = str_substr(md5(get_guid_v4()), 8);
        return "{$name}-{$hash}.{$ext}";
    }

}
